package com.linln.admin.system.web;

import com.github.pagehelper.Page;
import com.linln.common.utils.HttpServletUtil;
import com.linln.common.utils.ResultVoUtil;
import com.linln.common.vo.ResultVo;
import com.linln.modules.system.entity.BeautyCompany;
import com.linln.modules.system.entity.UserBill;
import com.linln.modules.system.service.BillServices;
import com.linln.modules.system.service.CompanyService;
import com.linln.modules.system.vo.admins.AdminUserBillVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "/mains/bill")
public class BillController extends BaseController {
    @Autowired
    private BillServices billServices;
    @Autowired
    private CompanyService companyService;

    @Autowired
    @Qualifier(value = "redisTemplate")
    private RedisTemplate<String,Object> defaultRedisTemplate;


    @RequestMapping(value = "/index")
    @RequiresPermissions("mains:bill:index")
    public ModelAndView getIndex(String startTime, String endTime, String nickName, Integer pageNum,Integer states) {
        ModelAndView view = new ModelAndView();
        view.setViewName("/mains/bill/index");
        Integer pageIndex = HttpServletUtil.getParameterInt("size", 1);
        pageNum = pageNum == null ? super.pageNum : pageNum;
        Page<AdminUserBillVo> pager = null;
        try {
            pager = billServices.getUserBillVoPager(pageNum, pageIndex*10, startTime, endTime, nickName,states);
        } catch (Exception e) {
            e.printStackTrace();
        }
        view.addObject("list", pager.getResult());
        view.addObject("page", pager);
        view.addObject("startTime", startTime);
        view.addObject("endTime", endTime);
        view.addObject("nickName", nickName);
        view.addObject("states", states);
        return view;
    }

    @GetMapping("/verify/{id}")
    public String toEdit(@PathVariable("id") String id, Model model) {
        UserBill userBill = billServices.getUserBillByKeyId(id);
        List<BeautyCompany> list = companyService.getCompanyList();
        model.addAttribute("company",list);
        model.addAttribute("billId", userBill.getId());
        return "/mains/bill/edit";
    }

    @GetMapping("/detail/{id}")
    public String teViewDetail(@PathVariable("id") String id, Model model) {
        UserBill userBill = billServices.getUserBillByKeyId(id);
        model.addAttribute("states", userBill.getStates());
        model.addAttribute("remark", userBill.getRemark());
        model.addAttribute("oldCode", userBill.getOldCode());
        model.addAttribute("amount", userBill.getAmount());
        return "/mains/bill/detail";
    }


    @ResponseBody
    @RequestMapping(value = "/save")
    public ResultVo toSave(String id, Integer states, String oldCode, BigDecimal amount, String remark,String prodId) {
        UserBill userBill = billServices.getUserBillByKeyId(id);
        Integer oldState = userBill.getStates();
        if (states == 1) {
            userBill.setRemark(remark).setStates(1);
        } else {
            userBill.setProdId(prodId);
            userBill.setOldCode(oldCode).setAmount(amount).setStates(2);
        }
        pushUserBillById(userBill.getUserId(),userBill.getId());
        Map<String, Object> map = billServices.updateUserBill(oldState, userBill);
        if (map.get("code") != Integer.valueOf(1)) {
            ResultVo success = ResultVoUtil.success(map.get("msg"));
            success.setData("submit[refresh]");
            return success;
        } else {
            ResultVo success = ResultVoUtil.error((String) map.get("msg"));
            return success;
        }
    }

    public Long pushUserBillById(String userId,String billId){
        ListOperations<String, Object> forList = defaultRedisTemplate.opsForList();
        Long push = forList.rightPush(userId, billId);
        return push;
    }
}
